//
//  MOAddressBookDetailViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/10.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOAddressBookDetailViewController.h"
#import "MOAddressBookDetailViewCell.h"
#import "MOSelectedMessageAndTelView.h"
#import "MOPhotoShowViewController.h"

@interface MOAddressBookDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)NSArray *titleArray;

@property(nonatomic,strong)UIView *hedaerView;

@property(nonatomic,strong)UIImageView *imageView;

@property(nonatomic,strong)UIImage *iconImage;

@end

@implementation MOAddressBookDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
    [self initData];
    
    [self getImage];
}

-(void)creatView
{
    self.title = @"通讯录详情";
    
    self.view.backgroundColor = KWhiteColor;
    
    [self.view addSubview:self.tableView];
    
    self.tableView.tableHeaderView = self.hedaerView;
}

-(void)initData
{
    NSArray *titleArray = @[
                        @[@"房间号"],
                        @[@"手机",@"IP电话",@"办公电话"],
                        @[@"电子邮件"],
                        @[@"邮编"]
                        ];
    self.dataSource = [NSMutableArray array];
    NSArray *valueArr = @[
                          @[self.addressModel.room],
                          @[self.addressModel.mobile,self.addressModel.ipphone,self.addressModel.tel],
                          @[self.addressModel.email],
                          @[self.addressModel.ybcode]
                          ];
    NSArray *colorArr = @[
                          @[KBlackColor_2A2A2A],
                          @[KBlueColor_2C94FF,KBlueColor_2C94FF,KBlueColor_2C94FF],
                          @[KBlueColor_2C94FF],
                          @[KBlackColor_2A2A2A]
                          ];
    
    for (int i = 0; i<valueArr.count; i++) {
        NSArray *arr1 = valueArr[i];
        NSArray *arr2 = titleArray[i];
        NSArray *arr3 = colorArr[i];
        NSMutableArray *muArr = [NSMutableArray array];
        for (int j = 0; j<arr2.count; j++) {
            MOAddressBookDetailModel *model = [MOAddressBookDetailModel alloc];
            model.nameStr = arr2[j];
            model.valueStr = arr1[j];
            model.valueColor = arr3[j];
            
            [muArr addObject:model];
        }
        [self.dataSource addObject:muArr];
    }
    [self.tableView reloadData];
}

-(void)getImage
{
    NSString *url = [NSString stringWithFormat:@"%@/%@",ADDRESSBOOK_IOREADIMAGE_URL,self.addressModel.userId];
    
    [PPNetworkHelper GET:url parameters:@{} success:^(id responseObject) {
        NSLog(@"头像：%@",responseObject);
        UIImage *image = [UIImage imageWithData:responseObject];
        if (image) {
            self.imageView.image = [UIImage imageWithData:responseObject];
            self.iconImage = [UIImage imageWithData:responseObject];
        }else{
            self.imageView.image = IMAGE(@"person_icon_b");
            self.iconImage = IMAGE(@"person_icon_b");
        }
        
    } failure:^(NSError *error) {
        NSLog(@"头像：%@",[error localizedDescription]);
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = self.dataSource[section];
    return arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOAddressBookDetailViewCell *cell = [MOAddressBookDetailViewCell cellWithTableView:tableView andIndexPath:indexPath];
    
    MOAddressBookDetailModel *model = self.dataSource[indexPath.section][indexPath.row];
    
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(54);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取cell在屏幕的位置
    CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:indexPath];
    CGRect rectInSuperview = [self.tableView convertRect:rectInTableView toView:[self.tableView superview]];
    NSLog(@"%f",rectInSuperview.origin.y);
    
    MOAddressBookDetailModel *model = self.dataSource[indexPath.section][indexPath.row];
    if (model.valueStr.length==0) {
        return;
    }
    if (indexPath.section==1&&indexPath.row==0) {
        MOSelectedMessageAndTelView *view = [[MOSelectedMessageAndTelView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        view.contentMaxY = rectInSuperview.origin.y + kHConversions(54) + kHConversions(54)/2 + 10;
        view.clickTelOrMessageButtonBlock = ^(NSInteger tag) {
            if (tag==1) {//打电话
                NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",model.valueStr];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
            }else{//发短信
                NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"sms://%@",model.valueStr];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
            }
        };
        [WINDOW addSubview:view];
    }
}


#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


-(NSMutableAttributedString *)contentString:(NSString *)str subString:(NSString *)subStr
{
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    NSMutableAttributedString *setString = [[NSMutableAttributedString alloc] initWithString:str];
    
    NSRange range = NSMakeRange([str rangeOfString:subStr].location, [str rangeOfString:subStr].length);
    
    [setString  addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    
    [setString addAttribute:NSForegroundColorAttributeName value:KBlackColor_919395 range:range];
    
    [setString addAttribute:NSFontAttributeName value:FONT_SIZE(16) range:range];
    
    return setString;
}

-(void)clickIcon
{
    MOPhotoShowViewController *vc = [[MOPhotoShowViewController alloc]init];
    if (self.iconImage) {
        vc.iconImage = self.iconImage;
    }else{
        vc.iconImage = IMAGE(@"person_icon_b");
    }
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark -- 懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = KGrayColor_F3F3F3;
        _tableView.separatorColor = KGrayColor_F3F3F3;
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

-(UIView *)hedaerView
{
    if (!_hedaerView) {
        _hedaerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10 + kWConversions(183))];
        _hedaerView.backgroundColor = KGrayColor_F3F3F3;
        
        UIView *infoView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, kWConversions(183))];
        infoView.backgroundColor = KWhiteColor;
        [_hedaerView addSubview:infoView];
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(kWConversions(15), kWConversions(17) + 10, kWConversions(110), kWConversions(183) - kWConversions(17)*2)];
        imageView.layer.masksToBounds = YES;
        imageView.layer.cornerRadius = 5.0;
        imageView.image = IMAGE(@"person_icon_b");
        imageView.userInteractionEnabled = YES;
        [_hedaerView addSubview:imageView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickIcon)];
        [imageView addGestureRecognizer:tap];
        
        self.imageView = imageView;
        
        
        
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(imageView.right + kWConversions(11), imageView.y + kWConversions(29), SCREEN_WIDTH - kWConversions(15)*3-imageView.width, 23)];
        nameLabel.font = [UIFont boldSystemFontOfSize:22];
        nameLabel.textColor = KBlackColor_2A2A2A;
        //@"王冠心 局长"
        nameLabel.attributedText = [self contentString:[NSString stringWithFormat:@"%@ %@",self.addressModel.nameStr,self.addressModel.post] subString:self.addressModel.post];
        [_hedaerView addSubview:nameLabel];
        
        UILabel *sectionLabel = [[UILabel alloc]initWithFrame:CGRectMake(imageView.right + kWConversions(11), nameLabel.bottom + kWConversions(28), SCREEN_WIDTH - kWConversions(15)*3-imageView.width, 18)];
        sectionLabel.font = FONT_SIZE(17);
        sectionLabel.text = self.addressModel.orig_name;
        sectionLabel.textColor = KBlackColor_2A2A2A;
        [_hedaerView addSubview:sectionLabel];
        
        UILabel *sectionOLabel = [[UILabel alloc]initWithFrame:CGRectMake(imageView.right + kWConversions(11), sectionLabel.bottom + kWConversions(10), SCREEN_WIDTH - kWConversions(15)*3-imageView.width, 18)];
        sectionOLabel.font = FONT_SIZE(14);
        sectionOLabel.text = self.addressModel.sectionStr;
        sectionOLabel.textColor = KBlackColor_2A2A2A;
        [_hedaerView addSubview:sectionOLabel];
    }
    return _hedaerView;
}




@end
